<template>
  <div>
    <!-- 3.s使用isAdd 展示添加还是编辑 -->
    <el-dialog :title="info.isAdd?'管理员添加':'管理员编辑'" :visible.sync="info.isshow" @closed="closed">
      {{form}}
      <el-form :model="form">
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="form.roleid">
            <el-option label="请选择" value disabled></el-option>
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名称" label-width="120px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="提示" label-width="120px" v-if="!info.isAdd">密码不写则不修改</el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqrolelist,
  requseradd,
  requseredit,
  requserinfo,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  components: {},
  props: ["info", "list"],
  data() {
    return {
      // 1. 初始化数据
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 2,
      },
      roleList: [], //角色的list
    };
  },
  mounted() {
    // 2.请角色list
    reqrolelist().then((res) => {
      if (res.data.code == 200) {
        this.roleList = res.data.list;
      }
    });
  },
  computed: {
    ...mapGetters({
      user: "info",
    }),
  },
  methods: {
    ...mapActions({
      changeInfo: "changeInfo",
    }),
    //4.取消
    cancel() {
      this.info.isshow = false;
    },
    //弹框关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    //5.重置form
    empty() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 2,
      };
    },
    //4.添加
    add() {
      requseradd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功了
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.$emit("init");
        }
      });
    },

    //获取一条数据
    getOne(uid) {
      requserinfo({ uid: uid }).then((res) => {
        if (res.data.code == 200) {
          // {menus: "[20,24,21,23,25]",rolename: "chaoguan",status: 1}
          this.form = res.data.list;

          //密码清空
          this.form.password = "";
        }
      });
    },
    //修改
    update() {
      requseredit(this.form).then((res) => {
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);

          //如果修改的管理就是当前登录的账号，那么需要重新登录
          if (this.user.uid === this.form.uid) {
            //清除用户数据
            this.changeInfo({});
            //退出登录
            this.$router.replace("/login");
            return;
          }

          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.$emit("init");
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
</style>